package com.example.xianji.activity;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xianji.Contant;
import com.example.xianji.R;

public class Main_Regesiter_OneActivity extends Activity {
    private Button next_step;
    private EditText editText_phone;
    private TextView register_text_login, change_phone_title;
    private ImageView register_one_back;
    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__regesiter__one);
        //	MyApplication.getInstance1().addActivity1(this);

        next_step = (Button) findViewById(R.id.next_step);
        editText_phone = (EditText) findViewById(R.id.editText_phone);
        register_text_login = (TextView) findViewById(R.id.register_text_login);
        register_one_back = (ImageView) findViewById(R.id.register_one_back);
        change_phone_title = (TextView) findViewById(R.id.change_phone_title);

        if (getIntent().getIntExtra("from", 0) == 1) {

            change_phone_title.setText("注册");
            register_text_login.setVisibility(View.VISIBLE);
        } else if (getIntent().getIntExtra("from", 0) == 2) {
            //编辑信息页面跳转
            change_phone_title.setText("修改手机号");
            register_text_login.setVisibility(View.INVISIBLE);
        } else if (getIntent().getIntExtra("from", 0) == 3) {
            //从登录页面跳转到编辑页面再新增手机号
            change_phone_title.setText("新增手机号");
            register_text_login.setVisibility(View.INVISIBLE);
        }

        editText_phone.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 11) {
                    phone = s.toString();
                    next_step.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_style));
                    next_step.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            if (getIntent().getIntExtra("from", 0) == 1) {
                                Intent intent = new Intent(Main_Regesiter_OneActivity.this, Main_Regester_twoActivity.class);
                                intent.putExtra("phone", phone);
                                startActivity(intent);
                                Main_Regesiter_OneActivity.this.finish();
                            } else if (getIntent().getIntExtra("from", 0) == 2) {
                                //修改手机号跳转到手机号验证页面
                                Intent intent = new Intent(Main_Regesiter_OneActivity.this, Main_Change_phone_numberActivity.class);
                                intent.putExtra("phone", phone);
                                intent.putExtra("isMobile", Contant.has_mobile);
                                startActivity(intent);
                                Main_Regesiter_OneActivity.this.finish();
                            } else if (getIntent().getIntExtra("from", 0) == 3) {
                                //新增手机号跳转到手机号验证页面
                                Intent intent = new Intent(Main_Regesiter_OneActivity.this, Main_Change_phone_numberActivity.class);
                                intent.putExtra("phone", phone);
                                intent.putExtra("isMobile", Contant.not_mobile);
                                intent.putExtra("xianji_token", getIntent().getStringExtra("xianji_token"));
                                startActivity(intent);
                                Main_Regesiter_OneActivity.this.finish();
                            }
                        }
                    });
                } else {
                    if (next_step != null) {
                        next_step.setBackgroundDrawable(getResources().getDrawable(R.drawable.regiseter_huise_style));
                    }
                }
            }
        });


        register_text_login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(Main_Regesiter_OneActivity.this, Main_LoginActivity.class));
            }
        });

        register_one_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_Regesiter_OneActivity.this.finish();
            }
        });
    }
}
